
/*
 * =====================================================================================
 *       Filename:  TTSServiceImpl
 *
 *    Description:
 *
 *
 *        Version:  1.0
 *        Created:  03/06/19 15:59:09
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  liushuyong (), shuyong1999@163.com
 *        Company:  shuyong
 *
 * =====================================================================================
 */

#include "TTSServiceImpl.h"
#include "RequestSession.h"
#include "RequestObj.h"
#include "TTSEngineMgr.h"

//DEFINE_string(models_folder, "", "tts engine model path.");
//DECLARE_string(models_folder);
//

TTSServiceImpl* TTSServiceImpl::gmInstance = new TTSServiceImpl();

TTSServiceImpl::TTSServiceImpl() {
#ifdef Debug
    LOG(INFO) << "New" << endl;
#endif
    mTTSEngineVersion = "2.0.0";
}

TTSServiceImpl::~TTSServiceImpl() {
#ifdef Debug
    LOG(INFO) << "Del" << endl;
#endif
}

bool TTSServiceImpl::loadResouce(const string& modelPath) {
    LOG(INFO) << "loadResouce=" << modelPath << endl;
    return TTSEngineMgr::loadResouce(modelPath);
}

TTSServiceImpl* TTSServiceImpl::getInstance() {
    return gmInstance;
}

string TTSServiceImpl::getTTSEngineVersion() {
    return mTTSEngineVersion;
}

/**
 * @brief 数据接收函数
 *
 * @param cntl_base 包含了所有request和response之外的参数集合
 * @param request  请求，只读的，来自client端的数据包。
 * @param response 回复。需要用户填充，如果存在required字段没有被设置，该次调用会失败。
 * @param done done由框架创建，递给服务回调，包含了调用服务回调后的后续动作，包括检查response正确性，序列化，打包，发送等逻辑。
               不管成功失败，done->Run()必须在请求处理完成后被用户调用一次。

 */

//TestSPtr gspTestSPtr;
void TTSServiceImpl::onTTS(RpcController* cntl_base, const HttpRequest* request, HttpResponse* response, Closure* done) {
    brpc::ClosureGuard done_guard(done);    //// 这个对象确保在return时自动调用done->Run(), 控制同步或异步服务
    brpc::Controller* rpcCntl = static_cast < brpc::Controller* >(cntl_base);   //强制类型转换

    if (rpcCntl->http_request().method() == brpc::HTTP_METHOD_POST) {   //处理请求
#ifdef Debug
        LOG(INFO) << "from:" << rpcCntl->remote_side() << endl;
#endif
        RequestObjSPtr spRequestObj = make_shared < RequestObj > ();    //智能指针,生成请求结构体

        if (NULL == spRequestObj.get()) {
            LOG(FATAL) << "shared_ptr RequestObj failed." << endl;
            abort();
        }

        spRequestObj->newContxtInfo(rpcCntl, done); //设置上下文资源

        if (spRequestObj->parseRequest() == 0) {    //解析请求
            spRequestObj->handleRequest();  //执行请求
        } else {
            LOG(ERROR) << "parseRequest error" << endl;
        }
    } else {       //在原有代码中有_healthcheck，没啥大用 del 2019-12-16 需要的时候再打开
        LOG(ERROR) << "Not Post Req" << endl;
    }

    done_guard.release();   // 异步服务,没有这个是同步服务
#ifdef Debug
    //LOG(INFO) << "onHttpRequest end" << endl;
#endif
}

void TTSServiceImpl::onCmd(::google::protobuf::RpcController* controller, const::CmdReq* request, ::CmdRsp* response, ::google::protobuf::Closure* done) {
}

void TTSServiceImpl::onStatus(::google::protobuf::RpcController* controller, const::StatusReq* request, ::StatusRsp* response, ::google::protobuf::Closure* done) {
}

/* *
 * 错误统计
 * */
